<?php

ini_set("display_errors",1);
include($_SERVER['DOCUMENT_ROOT'].'/includes/functions.inc.php');
include($_SERVER['DOCUMENT_ROOT'].'/config.php');
include($_SERVER['DOCUMENT_ROOT'].'/groups/database.php');
include($_SERVER['DOCUMENT_ROOT'].'/filter.libs.php');

$bits = explode('.',$_SERVER['SERVER_NAME']);
$domain = $bits[0];

$row = getRow("SELECT portal_id,title,user_id,realname,readable,gridsquares FROM portal INNER JOIN user USING (user_id) WHERE domain = ".dbQuote($domain)." AND status_id = 1");

if (empty($row['portal_id'])) {
        header("HTTP/1.0 404 Not Found");
        print "<html><head>";
        print "<title>Not found!</title>";
        print "</head><body>";
        print "<p>The page you are looking for is not available</p>";

        print "<p><a href=\"http://www.geographs.org/portals/?ref=$domain\">Try the Portal Homepage</a></p>";
        print "</body></html>";
        print str_repeat(" ",500);
        exit;
}

$id = intval($row['portal_id']);
$title = $row['title'];

$row['count'] = count_active($id);

include($_SERVER['DOCUMENT_ROOT'].'/header.inc.php');


if ($row['count']) {
	
	$images = getCol("SELECT DISTINCT comment FROM  p2.gridimage{$id}_bi_view WHERE status_id = 1 AND comment != '' LIMIT 100");
	
	print "<br/><div style=\"margin-left:auto;margin-right:auto;width:60%;color:brown;font-size:0.8em\">Just in case its not clear, the text on this page is totally frivolous, and not to be taken seriouslly.</div>";

	print "<h3>Dissociated Text about ".he($row['title'])."</h3>";

	$corpus = str_replace('.. ','. ',implode('. ',$images));

	$d = new dissociatedpress();	

	print "<p>".$d->dissociate($corpus,true,8,32)."</p>";

	$new = $d->dissociate($corpus,true,4,64);
	if ($words = getCol("SELECT DISTINCT label FROM gridimage_group WHERE portal_id = $id LIMIT 15")) {
		foreach ($words as $word) {
			$link = linktoself(array('cluster2'=>$word),'/');
                        $new = preg_replace('/('.preg_quote($word,'/').')/i',"<a href=\"$link\">\$1</a>",$new);
			
		}
	}
	print "<p>".$new."</p>";

	print "<p>In conclusion, ".$d->dissociate($corpus,true,16,8)."</p>";
 
	print "<hr/>&middot; <a href=\"dissociated.php\" style=\"background-color:silver;color:black;border:1px solid gray;padding:3px\">Go again!</a> &lt;- get another set of random text<hr/>";

	
	print "<br><p><small>Text by <a href='http://csiuo.com/drupal/content/dissociated-press-php'>Dissociated Press</a>, credit for the source corpus belongs to the <a 
href=\"http://oasthouse.portal.geographs.org/breakdown.php?view=text&by=realname&order=name&in=a\">contributors of images to this portal</a>, and released under a <a 
href=\"http://creativecommons.org/licenses/by-sa/3.0/\">Creative Commons Licence</a> (should you wish to reuse this text!).</small></p>";

} else {
	print "no content to show";
}





#############

include($_SERVER['DOCUMENT_ROOT'].'/footer.inc.php');













 
/**
 * dissociated-press.php
 *
 * @author     David Pascoe-Deslauriers <dpascoed@csiuo.com>
 * @copyright  2009 David Pascoe-Deslauriers
 * @license    http://www.csiuo.com/license/bsd.html  Simplified BSD License
 * @link       http://www.csiuo.com/drupal/node/13
 */
 
 
class dissociatedpress {
 
function dissociate ($str, $randomstart = true, $groupsize = 4, $max = 128) {
	if ($groupsize < 2) {
		$groupsize = 2;
	}
		// Capitalize the first word
	$capital = true;
 
		//Remove from corpus, they just make the result confusing
	$str = str_replace(array("(",")","[","]","{","}"), array(),$str);
 
		//Break up tokens
	$tokens = preg_split("/[ \r\n\t]/",$str);
 
		//Clean up token array
	for ($i = 0; $i < sizeof($tokens); $i++){
		if ($tokens[$i] == ""){
			unset($tokens[$i]);
		}
	}
 
	$tokens = array_values($tokens);
 
		//Init variables
	$return = "";
	$lastmatch = array();
 
		// if we start at the beginning, start there
	if (!$randomstart) {
		for ($n = 0; $n < $groupsize; $n++){
			array_push($lastmatch,$tokens[$n]);
			$res = cleanToken($tokens[$n],$capital);
			$return .= $res[0];
			$capital = $res[1];
		}
	}
 
		//Loop until we have enough output
	$i = 0;
	while ($i < $max + 32){
			// Try and end on a full sentence
		if ($i > $max - 8 and $capital){
			break;
		}
 
			//If the lastmatch group isn't good enough, start randomly
		if (sizeof($lastmatch) < $groupsize){
			$loc = rand(0,sizeof($tokens)-$groupsize);
			$lastmatch = array();
			for ($n = 0; $n < $groupsize; $n++){
				array_push($lastmatch,$tokens[$loc+$n]);
				$res = dissociatedpress::cleanToken($tokens[$loc+$n],$capital);
				$return .= $res[0];
				$capital = $res[1];
			}
		} else {
			$chains = dissociatedpress::findChains($tokens, $lastmatch);
			$lastmatch = array();
 
				// If there aren't enough chains, start randomly next time (avoid getting caught in loops)
			if (sizeof($chains) > 2) {
				$loc = $chains[rand(0, sizeof($chains)-1)];
				for ($n = 0; $n < $groupsize; $n++){
					array_push($lastmatch,$tokens[$loc+$n]);
						$res = dissociatedpress::cleanToken($tokens[$loc+$n],$capital);
					$return .= $res[0];
					$capital = $res[1];
				}
			}
		}
		$i++;
	}
 
	return $return;
}
 
/**
 * Join the tokens with proper typography
 */
 
function cleanToken($token,$capital) {
	if ($capital){
		$token = ucfirst($token);
		$capital = false;
	}
 
	if (substr($token,-1,1) == '.'){
		$capital = true;
		return array($token . "  ",$capital);
	} else {
		return array($token . " ",$capital);
	}
}
 
/**
 * Naively find possible Markov Chains
 */
 
function findChains($haystack, $needle) {
	$return = array();
	for ($i = 0; $i < sizeof($haystack) - sizeof($needle); $i++){
		if ($haystack[$i] == $needle[0]){
			$matches = true;
			for ($j = 1; $j < sizeof($needle); $j++){
				if ($haystack[$i+$j] != $needle[$j]){
					$matches = false;
					break;
				}
			}
			if ($matches == true){
				array_push($return,$i+sizeof($needle));
			}
		}
	}
	return $return;
}
 
}
?>
